//
//  AlertDemo.swift
//  SwiftUIDemo
//
//  Created by Tilt Wang on 2025/6/29.
//

import SwiftUI

struct AlertDemo: View {
    
    @State var showAlert: Bool = false
    @State var backgroundColor: Color = .red
    @State var alertType: MyAlerts = .success
//    @State var alertTitle: String = ""
//    @State var alertMessage: String = ""
    
    enum MyAlerts {
        case success
        case error
    }
    
    var body: some View {
        ZStack {
            backgroundColor.edgesIgnoringSafeArea(.all)
            Button("Click here") {
                showAlert.toggle()
            }
            .alert(isPresented: $showAlert) {
                getAlert()
            }
            
            VStack {
                Spacer(minLength: 40)
                Button("Button 1") {
//                    alertTitle = "Error 1"
//                    alertMessage = "Message 1"
                    alertType = .success
                    showAlert.toggle()
                }
                Button("Button 2") {
//                    alertTitle = "Warning 2"
//                    alertMessage = "Message 2"
                    alertType = .error
                    showAlert.toggle()
                }
            }
            
        }
    }
    
    func getAlert() -> Alert {
        var alertTitle: String = ""
        var alertMessage: String = ""
        
        switch alertType {
        case .success:
            alertTitle = "Success"
            alertMessage = "Success message"
        case .error:
            alertTitle = "Error"
            alertMessage = "Error message"
        }
        
        return Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("OK")))
//        return Alert(title: Text("This is the title"),
//                     message: Text("Here we will describe the error"),
//                     //primaryButton: .destructive(Text("Delete")),
//                     primaryButton: .destructive(Text("Delete"), action: {
//            backgroundColor = .green
//        }),
//                     secondaryButton: .cancel()
//        )
    }
    
}

#Preview {
    AlertDemo()
}
